package com.huilian.iotbox.admin.wap.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.huilian.iotbox.admin.wap.dao.StatsCommodityStockRecordDao;
import com.huilian.iotbox.admin.wap.service.CommodityStockRecordService;
import com.huilian.iotbox.admin.wap.service.StatsCommodityStockRecordService;
import com.huilian.iotbox.data.dto.CommodityStockRecordDto;
import com.huilian.iotbox.data.dto.StatsCommodityStockRecordDto;
import com.huilian.iotbox.data.dto.StatsDeviceDto;
import com.huilian.iotbox.data.utils.DateTimeUtils;
import com.huilian.iotbox.data.vo.CommodityStockRecordVo;
import com.huilian.iotbox.data.vo.StatsCommodityStockRecordVo;
import com.huilian.iotbox.data.vo.StatsDeviceVo;
import com.huilian.iotbox.data.vo.StatsUserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author 龙 路 飞
 * @CreateDate: 2023-08-31 17:55
 * @Description:
 */
@Service
public class StatsCommodityStockRecordServiceImpl implements StatsCommodityStockRecordService {

    @Resource
    private StatsCommodityStockRecordDao statsCommodityStockRecordDao;
    @Autowired
    private CommodityStockRecordService commodityStockRecordService;

    /**
     * 根据条件查询统计商品库存集合
     */
    @Override
    public PageInfo findStatsCommodityStockRecordList(StatsCommodityStockRecordDto statsCommodityStockRecordDto) {
        switch (statsCommodityStockRecordDto.getDayType()) {
            case 4:
                //今年
                statsCommodityStockRecordDto.setStartDateStr(DateTimeUtils.getYearFirstDayTimeYM());
                statsCommodityStockRecordDto.setEndDateStr(DateTimeUtils.getMonthYM());
                statsCommodityStockRecordDto.setType(2);
                break;
            case 5:
                //上月
                //根据日期范围查询
                statsCommodityStockRecordDto.setStartDateStr(DateTimeUtils.getPreviousMonthYM());
                statsCommodityStockRecordDto.setEndDateStr(DateTimeUtils.getPreviousMonthYM());
                statsCommodityStockRecordDto.setType(2);
                break;
            default:
                statsCommodityStockRecordDto.setType(1);

                break;
        }

        PageInfo pageInfo = new PageInfo();
        if(!ObjectUtils.isEmpty(statsCommodityStockRecordDto.getStartDateStr()) && !ObjectUtils.isEmpty(statsCommodityStockRecordDto.getEndDateStr())){
            final Integer hour = this.hourTime();
            if(statsCommodityStockRecordDto.getStartDateStr().equals(DateTimeUtils.getCurrentDateStr())||
                    (hour >= 0 && hour <= 5 && statsCommodityStockRecordDto.getDayType().compareTo(1)==0)){
                // 判断开始时间是今天
                CommodityStockRecordDto commodityStockRecordDto = new CommodityStockRecordDto();
                if(hour >= 0 && hour <= 5 && statsCommodityStockRecordDto.getDayType().compareTo(1)==0){
                    //获取昨天的开始时间和结束时间 格式yyyy-MM-dd HH:mm:ss
                    commodityStockRecordDto.setStartTime(DateTimeUtils.parseLocalDate(DateTimeUtils.getYesterday()).format(DateTimeUtils.START_DATETIME_FORMATTER));
                    commodityStockRecordDto.setEndTime(DateTimeUtils.parseLocalDate(DateTimeUtils.getYesterday()).format(DateTimeUtils.END_DATETIME_FORMATTER));
                }else{
                    commodityStockRecordDto.setStartTime(DateTimeUtils.getTodayStartTime());
                    commodityStockRecordDto.setEndTime(DateTimeUtils.getTodayEndTime());
                }
                commodityStockRecordDto.setUserInfo(statsCommodityStockRecordDto.getUserInfo());
                commodityStockRecordDto.setSecondaryType(statsCommodityStockRecordDto.getSecondaryType());
                commodityStockRecordDto.setFirstType(statsCommodityStockRecordDto.getFirstType());
                commodityStockRecordDto.setMerchantStoreIdArray(statsCommodityStockRecordDto.getMerchantStoreIdArray());
                PageHelper.startPage(statsCommodityStockRecordDto.getPageNum(),statsCommodityStockRecordDto.getPageSize());

                List<CommodityStockRecordVo> commodityStockRecordList = commodityStockRecordService.findCommodityStockRecordListByTime(commodityStockRecordDto);
                pageInfo = new PageInfo(commodityStockRecordList);
            }else{
                PageHelper.startPage(statsCommodityStockRecordDto.getPageNum(),statsCommodityStockRecordDto.getPageSize());
                List<StatsCommodityStockRecordVo> statsCommodityStockRecordList = statsCommodityStockRecordDao.findStatsCommodityStockRecordList(statsCommodityStockRecordDto);
                pageInfo = new PageInfo(statsCommodityStockRecordList);
            }
        }
        return pageInfo;
    }

    /**
     * 根据条件查询统计数据总和
     */
    @Override
    public StatsCommodityStockRecordVo findStatsCommodityStockRecordSum(StatsCommodityStockRecordDto statsCommodityStockRecordDto) {
        switch (statsCommodityStockRecordDto.getDayType()) {
            case 4:
                //今年
                statsCommodityStockRecordDto.setStartDateStr(DateTimeUtils.getYearFirstDayTimeYM());
                statsCommodityStockRecordDto.setEndDateStr(DateTimeUtils.getMonthYM());
                statsCommodityStockRecordDto.setType(2);
                break;
            case 5:
                //上月
                //根据日期范围查询
                statsCommodityStockRecordDto.setStartDateStr(DateTimeUtils.getPreviousMonthYM());
                statsCommodityStockRecordDto.setEndDateStr(DateTimeUtils.getPreviousMonthYM());
                statsCommodityStockRecordDto.setType(2);
                break;
            default:
                statsCommodityStockRecordDto.setType(1);
                break;
        }
        StatsCommodityStockRecordVo statsCommodityStockRecordVo = new StatsCommodityStockRecordVo();
        if(!ObjectUtils.isEmpty(statsCommodityStockRecordDto.getStartDateStr()) && !ObjectUtils.isEmpty(statsCommodityStockRecordDto.getEndDateStr())){
            //凌晨0点到六点调用实时统计的方法
            final Integer hour = this.hourTime();
            if(statsCommodityStockRecordDto.getStartDateStr().equals(DateTimeUtils.getCurrentDateStr())||
                    (hour >= 0 && hour <= 5 && statsCommodityStockRecordDto.getDayType().compareTo(1)==0)){
                // 判断开始时间是今天
                CommodityStockRecordDto commodityStockRecordDto = new CommodityStockRecordDto();
                if(hour >= 0 && hour <= 5 && statsCommodityStockRecordDto.getDayType().compareTo(1)==0){
                    //获取昨天的开始时间和结束时间 格式yyyy-MM-dd HH:mm:ss
                    commodityStockRecordDto.setStartTime(DateTimeUtils.parseLocalDate(DateTimeUtils.getYesterday()).format(DateTimeUtils.START_DATETIME_FORMATTER));
                    commodityStockRecordDto.setEndTime(DateTimeUtils.parseLocalDate(DateTimeUtils.getYesterday()).format(DateTimeUtils.END_DATETIME_FORMATTER));
                }else{
                    commodityStockRecordDto.setStartTime(DateTimeUtils.getTodayStartTime());
                    commodityStockRecordDto.setEndTime(DateTimeUtils.getTodayEndTime());
                }
                commodityStockRecordDto.setUserInfo(statsCommodityStockRecordDto.getUserInfo());
                commodityStockRecordDto.setSecondaryType(statsCommodityStockRecordDto.getSecondaryType());
                commodityStockRecordDto.setMerchantStoreIdArray(statsCommodityStockRecordDto.getMerchantStoreIdArray());
                commodityStockRecordDto.setFirstType(statsCommodityStockRecordDto.getFirstType());
                CommodityStockRecordVo commodityStockRecordVo = commodityStockRecordService.findCommodityStockRecordSumByTime(commodityStockRecordDto);
                statsCommodityStockRecordVo.setTotalQuantity(commodityStockRecordVo.getTotalQuantity());
                statsCommodityStockRecordVo.setTotalCostPrice(commodityStockRecordVo.getTotalCostPrice());
                statsCommodityStockRecordVo.setTotalRetailPrice(commodityStockRecordVo.getTotalRetailPrice());
            }else{
                //昨天
                statsCommodityStockRecordVo = statsCommodityStockRecordDao.findStatsCommodityStockRecordSum(statsCommodityStockRecordDto);
            }
        }
        return statsCommodityStockRecordVo;
    }

    @Override
    public List<StatsDeviceVo> findStatsCommodityStockRecordDeviceTableList(StatsDeviceDto statsDeviceDto) {
        return statsCommodityStockRecordDao.findStatsCommodityStockRecordDeviceTableList(statsDeviceDto);
    }

    @Override
    public StatsDeviceVo findStatsCommodityStockRecordDevice(StatsDeviceDto statsDeviceDto) {
        return statsCommodityStockRecordDao.findStatsCommodityStockRecordDevice(statsDeviceDto);
    }

    //凌晨0点到六点调用实时统计的方法
    public Integer hourTime(){
        Date date = new Date();
        String pattern = "HH";
        Locale locale = Locale.getDefault();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern, locale);
        String time = simpleDateFormat.format(date);
        Integer hour = Integer.parseInt(time);
        return hour;
    }
}
